﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Reflection;

namespace Fcdbas.Platform.Properties
{
    [TypeConverter(typeof(ExtendOptionsConverter))]
    public class ExtendOptionsProperty
    {
        public ExtendOptionsProperty()
        {
            DeferedPopulate = false;
            StateMachine = true;
            EnableWarnings = false;
            GroupAndSum = true;
        }

        public ExtendOptionsProperty(bool[] options)
        {
            DeferedPopulate = options[0];
            StateMachine = options[1];
            EnableWarnings = options[2];
            GroupAndSum = options[3];
        }

        public ExtendOptionsProperty(bool deferedPopulate, bool stateMachine, bool enableWarnings, bool groupAndSum)
        {
            DeferedPopulate = deferedPopulate;
            StateMachine = stateMachine;
            EnableWarnings = enableWarnings;
            GroupAndSum = groupAndSum;
        }

        /// <summary>
        /// 未知
        /// </summary>
        public bool DeferedPopulate
        {
            get;
            set;
        }

        /// <summary>
        /// 是否开启状态机
        /// </summary>
        public bool StateMachine
        {
            get;
            set;
        }

        /// <summary>
        /// 是否开启警告
        /// </summary>
        public bool EnableWarnings
        {
            get;
            set;
        }

        /// <summary>
        /// 是否分组与汇总
        /// </summary>
        public bool GroupAndSum
        {
            get;
            set;
        }
    }

    public class ExtendOptionsConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string)) return true;

            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string)) return true;

            if (destinationType == typeof(InstanceDescriptor)) return true;

            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            string result = string.Empty;
            if (destinationType == typeof(string))
            {

                ExtendOptionsProperty options = value as ExtendOptionsProperty;
                if (options == null)
                    return string.Empty;

                result = "DeferedPopulate,StateMachine,EnableWarnings,GroupAndSum";

                if (!options.DeferedPopulate)
                    result = result.Replace("DeferedPopulate", "");
                if (!options.StateMachine)
                    result = result.Replace("StateMachine", "");
                if (!options.EnableWarnings)
                    result = result.Replace("EnableWarnings", "");
                if (!options.GroupAndSum)
                    result = result.Replace("GroupAndSum", "");

                return result;
            }

            if (destinationType == typeof(InstanceDescriptor))
            {
                //ConstructorInfo ci = typeof(ExtendOptionsProperty).GetConstructor(new Type[] { typeof(bool), typeof(bool), typeof(bool) });
                ExtendOptionsProperty options = (ExtendOptionsProperty)value;

                return new InstanceDescriptor(null, new object[] { options.DeferedPopulate, options.StateMachine, options.EnableWarnings, options.GroupAndSum });
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                string valueObject = value as string;
                if (string.IsNullOrEmpty(valueObject))
                    return new ExtendOptionsProperty();

                string[] optionsValue = valueObject.Split(',');
                if (optionsValue.Length != 4)
                    throw new ArgumentException("无效的属性格式");

                ExtendOptionsProperty options = new ExtendOptionsProperty(optionsValue[0].ToLower() == "deferedpopulate" ? true : false,
                                                                          optionsValue[1].ToLower() == "statemachine" ? true : false,
                                                                          optionsValue[2].ToLower() == "enablewarnings" ? true : false,
                                                                          optionsValue[3].ToLower() == "groupandsum" ? true : false);

                return options;
            }

            return base.ConvertFrom(context, culture, value);
        }

        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            return TypeDescriptor.GetProperties(typeof(ExtendOptionsProperty), attributes);
        }
    }
}
